package acm.pta3;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class T13 {
    /**
     * 思路：
     *  先构建一个搜索二叉树，
     *  层序遍历这棵二叉树，
     *  如果该二叉树存在以下情况，则不是完全二叉树
     *      1.存在有右无左的节点
     *      2.如果某个节点孩子不全，其后继节点不是叶子节点
     */
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt()-1;
        Tree root=new Tree(sc.nextInt());
        while (n-- > 0) {
            root.add(sc.nextInt());
        }
        levelOrder(root);
    }

    public static void levelOrder(Tree root){
        boolean isPerfect=true; //是否为完全二叉树
        boolean isLeaf=false;   //是否应该为叶子节点
        StringBuilder res=new StringBuilder();
        Queue<Tree> queue=new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Tree curr = queue.poll();
            if (curr==null){
                continue;
            }
            res.append(curr);
            Tree right = curr.right;
            Tree left = curr.left;

            if (right !=null&& left ==null){
                isPerfect=false;
            }

            if (isLeaf&&(left!=null||right!=null)){
                isPerfect=false;
            }
            queue.add(left);
            queue.add(right);
            isLeaf=right==null;
        }
        System.out.println(res.substring(0,res.length()-1));
        System.out.println(isPerfect?"YES":"NO");
    }

    static class Tree{
        int val;
        Tree left;
        Tree right;

        public Tree(int val) {
            this.val = val;
        }
        public void add(int val){
            Tree curr=this;
            Tree parent=null;
            while (curr!=null){
                parent=curr;
                curr=val> curr.val?curr.left:curr.right;
            }
            if (val> parent.val){
                parent.left=new Tree(val);
            }else {
                parent.right=new Tree(val);
            }
        }

        @Override
        public String toString() {
            return val+" ";
        }
    }
}
